<template>
  <div class="page-model">
    <el-dialog
      v-model="dialogVisible"
      :title="title"
      width="30%"
      center
      destroy-on-close
    >
      <crv-form
        v-bind="modelConfig"
        v-model="formData"
        ref="crvFormRef"
      ></crv-form>
      <slot></slot>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmBtnClick"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, PropType, watch } from 'vue'
import CrvForm, { IForm } from '@/base-ui/form'

import { useStore } from 'vuex'
export default defineComponent({
  name: 'page-model',
  components: {
    CrvForm
  },
  emits: ['formDataChange'],
  props: {
    modelConfig: {
      type: Object as PropType<IForm>,
      required: true
    },
    pageName: {
      type: String,
      required: true
    },
    defalultInfo: {
      type: Object,
      required: true
    },
    otherInfo: {
      type: Object,
      default: () => ({})
    }
  },
  setup(props, { emit }) {
    const crvFormRef = ref<InstanceType<typeof CrvForm>>()
    const formData = ref<any>({})
    const dialogVisible = ref(false)
    const title = ref('')
    const store = useStore()

    watch(
      () => props.defalultInfo,
      (newValue) => {
        for (const item of props.modelConfig.formItems) {
          formData.value[`${item.field}`] = newValue[`${item.field}`]
        }

        if (Object.keys(newValue).length > 0) {
          title.value = '编辑' + props.modelConfig.title
        } else {
          title.value = '新建' + props.modelConfig.title
        }

        emit('formDataChange', { newValue: formData.value, oldValue: {} })
      }
    )

    watch(formData, (newValue, oldValue) => {
      emit('formDataChange', { newValue, oldValue })
    })

    const confirmBtnClick = () => {
      crvFormRef.value?.ruleFormRef?.validate((valid) => {
        if (valid) {
          if (Object.keys(props.defalultInfo).length > 0) {
            console.log('编辑的数据是', formData.value)
            store
              .dispatch('system/editPageDataAction', {
                pageName: props.pageName,
                editData: { ...formData.value, ...props.otherInfo },
                id: props.defalultInfo.id
              })
              .then((res) => {
                dialogVisible.value = false
              })
          } else {
            console.log('新建的数据是', formData.value)
            store
              .dispatch('system/createPageDataAction', {
                pageName: props.pageName,
                newData: { ...formData.value, ...props.otherInfo }
              })
              .then((res) => {
                dialogVisible.value = false
              })
          }
        }
      })
    }

    return {
      title,
      formData,
      dialogVisible,
      crvFormRef,
      confirmBtnClick
    }
  }
})
</script>

<style scoped lang="less">
.page-model {
  .menu-tree {
    padding-left: 20px;
  }
}
</style>
